package models

import (
	"github.com/astaxie/beego/orm"
	"strconv"
)

type AiRoute struct {
	Id               int64
	Dialplan_name    string
	Dialplan_context string
	Dialplan_number  string
}

type AiRouteDetails struct {
	Id                      int64
	Dialplan_id             int64
	Dialplan_detail_tag     string
	Dialplan_detail_data    string
	Dialplan_detail_type_id int
}

type AiRouteList struct {
	Id                   int64
	Dialplan_name        string
	Dialplan_context     string
	Dialplan_detail_tag  string
	Dialplan_number      string
	Dialplan_detail_data string
}

func (a *AiRoute) TableName() string {
	return "call_dialplans"
}

func (a *AiRouteDetails) TableName() string {
	return "call_dialplan_details"
}

func GetRouteList(page, pageSize int) (int64, []AiRouteList, error) {
	limit := " "
	if page == 0 && pageSize == 0 {
	} else {
		limit = " LIMIT " + strconv.Itoa(pageSize) + " OFFSET " + strconv.Itoa((page-1)*pageSize)
	}
	sqlStr := "select a.id,a.dialplan_name,a.dialplan_context,a.dialplan_number,b.dialplan_detail_tag,b.dialplan_detail_data " +
		" from call_dialplans a ,call_dialplan_details b " +
		" where (b.dialplan_detail_type_id=27 or b.dialplan_detail_type_id=28) AND a.id=b.dialplan_id " + limit + ";"

	var cdrCount int64
	var list []AiRouteList
	cdrCount, err := orm.NewOrm().Raw(sqlStr).QueryRows(&list)
	if err != nil {
		ErrorLog(err)
		return 0, nil, err
	}

	return cdrCount, list, nil
}
func GetRoute(id int64) (AiRouteList, error) {
	sqlStr := "select a.id,a.dialplan_name,a.dialplan_context,a.dialplan_number,b.dialplan_detail_tag,b.dialplan_detail_data " +
		" from call_dialplans a ,call_dialplan_details b " +
		" where (b.dialplan_detail_type_id=27 or b.dialplan_detail_type_id=28) AND a.id=b.dialplan_id AND a.id=" + strconv.FormatInt(id,10) + ";"

	var route AiRouteList
	 err := orm.NewOrm().Raw(sqlStr).QueryRow(&route)
	if err != nil {
		ErrorLog(err)
		return AiRouteList{}, err
	}

	return  route, nil
}

func InsertRoute(p *AiRoute) (int64, error) {
	return orm.NewOrm().Insert(p)
}

func InsertRouteDetail(p *AiRouteDetails) (int64, error) {
	return orm.NewOrm().Insert(p)
}

func UpdateRoute(id int64,Dialplan_number ,Dialplan_name,tag,data string) (error) {
	splStr1 := " Update call_dialplans set Dialplan_number='" + Dialplan_number + "',Dialplan_name='"+ Dialplan_name +
		"' WHERE id=" + strconv.FormatInt(id, 10) + " ;"
	_, err := orm.NewOrm().Raw(splStr1).Exec()
	if err != nil {
		return err
	}

	splStr := "Update  call_dialplan_details set Dialplan_detail_tag='" +tag + "',Dialplan_detail_data='"+ data  +
		"' WHERE dialplan_id=" + strconv.FormatInt(id, 10) + " and dialplan_detail_type_id=27;"
	_, err = orm.NewOrm().Raw(splStr).Exec()
	if err != nil {
		return err
	}
	return nil
}


func DelectRoute(id int64) (error) {
	splStr1 := " DELETE FROM call_dialplan_details " +
		" WHERE dialplan_id=" + strconv.FormatInt(id, 10) + " and dialplan_detail_type_id=27;"
	_, err := orm.NewOrm().Raw(splStr1).Exec()
	if err != nil {
		return err
	}

	splStr := "DELETE FROM call_dialplans WHERE id=" + strconv.FormatInt(id, 10) + " ;"
	_, err = orm.NewOrm().Raw(splStr).Exec()
	if err != nil {
		return err
	}
	return nil
}
